package com.yy.controller.user;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.common.core.AjaxResult;
import com.yy.common.core.HttpStatus;
import com.yy.common.core.model.DishCommentVM;
import com.yy.common.core.page.TableDataInfo;
import com.yy.common.utils.PageHelper;
import com.yy.pojo.DishComment;
import com.yy.pojo.User;
import com.yy.service.DishCommentService;
import com.yy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dishes-comment")
public class DishCommentController {
    @Autowired
    private DishCommentService dishCommentService;

    @Autowired
    private UserService userService;

    @GetMapping("/query")
    public TableDataInfo queryAllComment(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                         DishComment dishComment) {

        LambdaQueryWrapper<DishComment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishComment::getDishId, dishComment.getDishId());
        lambdaQueryWrapper.orderByDesc(DishComment::getCreateTime);

        IPage<DishComment> page = dishCommentService.page(new Page<>(pageNum, pageSize), lambdaQueryWrapper);
        List<User> userList = userService.list();
        Page<DishCommentVM> dishCommentVMPage = PageHelper.copyMap(page, d -> {
            DishCommentVM dishCommentVM = new DishCommentVM();
            BeanUtil.copyProperties(d, dishCommentVM);
            userList.stream().filter(u -> d.getUserId().equals(u.getUserId())).findFirst()
                    .ifPresent(u -> {
                        dishCommentVM.setAvatar(u.getAvatar());
                        dishCommentVM.setNikeName(u.getNickName());
                    });
            dishCommentVM.setCreateTime(DateUtil.format(d.getCreateTime(), "yyyy年MM月dd日 HH:mm:ss"));
            dishCommentVM.setCommentContent(new String(d.getCommentContent()));
            return dishCommentVM;
        });

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(dishCommentVMPage.getRecords());
        rspData.setTotal(dishCommentVMPage.getTotal());
        return rspData;
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody DishCommentVM dishCommentVM) {

        DishComment dishComment = new DishComment();
        dishComment.setDishId(dishCommentVM.getDishId());
        dishComment.setCommentContent(dishCommentVM.getCommentContent().getBytes());
        dishComment.setCommentScore(dishCommentVM.getCommentScore());
        dishComment.setUserId(dishCommentVM.getUserId());
        dishComment.setCreateTime(DateUtil.date());
        dishCommentService.save(dishComment);

        dishCommentVM.setCommentId(dishComment.getCommentId());
        dishCommentVM.setCreateTime(DateUtil.format(dishComment.getCreateTime(), "yyyy年MM月dd日 HH:mm:ss"));

        User user = userService.getById(dishCommentVM.getUserId());
        dishCommentVM.setNikeName(user.getNickName());
        dishCommentVM.setAvatar(user.getAvatar());

        return AjaxResult.success(dishCommentVM);
    }

}
